package com.saibo.spider.gather;

import com.saibo.spider.pojo.AutohomeBBSPostInfo;
import com.saibo.spider.pojo.AutohomeBBSUserinfo;
import com.saibo.spider.utils.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MouldBBSAutohomeInfo implements CrawlerInterface{
	
	Logger log = Logger.getLogger(MouldBBSAutohomeInfo.class);
	Map<String,Object> map;
	String url ;
	String html;
	Submitable submitobj = null;
	public void startMission(String url, String mapInfo,Submitable submitobj) {
		// TODO Auto-generated method stub
		this.submitobj = submitobj;
		map = StringUtil.jsonToMap(mapInfo);
		log.info("【请求类MouldBBSAutohomeInfo】接收任务URL:"+url+"\t附加参数:"+map.get("addtionalParameter").toString()+"\t分页数:"+map.get("pageNumber").toString());
		
		for (int i = 0 ; i < 5 ;i++) {
			
		}
	}

	public void requestUrl() {
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, requestHeader("http://club.autohome.com.cn/bbs/forum-c-770-1.html"));
		html = HttpResponseUtil.getHtml(httpResponse, "gb2312");
	}

	public void parse() {
		AutohomeBBSPostInfo postinfo = new AutohomeBBSPostInfo();// 存放帖子内容实体
		Document document = Jsoup.parse(html);// 格式化网页代码
		Element cont_main_ele = document.select("#maxwrap-maintopic").first(); // 主贴位置
		// 解析帖子点击回复
		Element consnavEle = document.select("#consnav").first();
		//获取论坛地址，生成论坛rowkey
		Element classifyElement = consnavEle.select("div>span>a").first();
		String classifyUrl = "http://club.autohome.com.cn/bbs/"+classifyElement.attr("href");//论坛地址
		Element clickEle = consnavEle.select("#x-views").get(0);// 点击
		Element replysEle = consnavEle.select("#x-replys").get(0);// 回复
		Element titleEle = consnavEle.select("div>span").last();// 标题
		String title = JsoupUtil.element2text(titleEle);// 标题
		String click = JsoupUtil.element2text(clickEle);// 点击数
		String replys = JsoupUtil.element2text(replysEle);// 回复数
		// 解析个人信息
		Elements author_ele = cont_main_ele.select("div>div>div.conleft>ul.maxw>li");
		for (Element element : author_ele) {
			Element uname = element.select("li>a[xname=uname]").first();
			if (!StringUtil.isNullOrEmpty(uname)) {
				// 开始解析用户名以及用户主页地址
				// String author_url = uname.attr("href");
			}
			Element crade = element.select("li>a.crade").first();
			if (!StringUtil.isNullOrEmpty(crade)) {
				System.out.println(crade.attr("title"));
			}
		}
		// 主贴
		Element mainPost = cont_main_ele.select("div>div.clearfix").first();
		//获取是否是使用平台发帖
		Elements platform_ele = mainPost.select("div>div.conright>div>span");
		postinfo.setPlatform("");
		for (Element element : platform_ele) {
			if(element.toString().contains("来自")){
				Element platform_str_ele = element.select("span>a").first();
				postinfo.setPlatform(StringUtil.element2text(platform_str_ele));
			}
		}
		String create_date_str = mainPost.attr("data-time");
		String pk_str = mainPost.attr("pk");
		String uid_str = mainPost.attr("uid");
		Element content_ele = mainPost.select("div>div.conright>div.rconten>div.conttxt>div").first();
		postinfo.setContent(StringUtil.element2text(content_ele));
		postinfo.setTitle(title);
		postinfo.setUrl(url);
		postinfo.setClick_num(Integer.parseInt(click));
		postinfo.setReply_num(Integer.parseInt(replys));
		postinfo.setRowkey(MD5Util.GET16BITMD5(url));
		postinfo.setPk(pk_str);
		postinfo.setUid(uid_str);
		postinfo.setBrand_rowkey(MD5Util.GET16BITMD5(classifyUrl));
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date;
		try {
			date = format.parse(create_date_str);
			postinfo.setCreate_date(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		parseAuthor(html, postinfo);
		parseReply(html);
		System.err.println(StringUtil.objectToJson(postinfo));
		Element nextPage = document.select("#content>div.pagearea>div.pages>div.afpage").first();
		if(!StringUtil.isNullOrEmpty(nextPage))url = "http://club.autohome.com.cn/bbs/"+nextPage.attr("href");
	}

	public void submit() {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 解析作者用户信息
	 */
	public void parseAuthor(String html, AutohomeBBSPostInfo postinfo) {
		Element divConleft = Jsoup.parse(html).select("#maxwrap-maintopic>div.clearfix>div.conleft").first();
		Element username_ele = divConleft.select("div>ul.maxw>li.txtcenter>a[xname=uname]").first();
		String author_nickname = username_ele.attr("title");
		String url = username_ele.attr("href");
		postinfo.setAuthor_rowkey(MD5Util.GET16BITMD5(url));
		AutohomeBBSUserinfo userinfo = new AutohomeBBSUserinfo();
		if (StringUtil.isNullOrEmpty(divConleft.select("div>ul.maxw>li.txtcenter>a[title=认证车主]"))) {
			userinfo.setIs_authentication(0);
		} else {
			userinfo.setIs_authentication(1);
		}
		userinfo.setNickname(author_nickname);
		userinfo.setUrl(url);
		userinfo.setRowkey(MD5Util.GET16BITMD5(url));

		Elements otherEle = divConleft.select("div>ul.maxw>li.txtcenter>div.lv_card>span.identity");
		if (!StringUtil.isNullOrEmpty(otherEle)) {
			userinfo.setJob(StringUtil.element2text(otherEle.first()));
		}
		// 解析用户信息
		Element otherUserIndex = divConleft.select("#userprofileimg").first();
		String userimg = otherUserIndex.attr("src");
		userinfo.setHead_img(userimg);
		Elements jh_ele = divConleft.select("div>ul.leftlist>li");
		for (Element element : jh_ele) {
			if (element.toString().contains("精华：")) {
				String jh = StringUtil.element2text(element.select("li>a").first()).replaceAll("帖", "").replaceAll(" ",
						"");
				if(StringUtil.isNullOrEmpty(jh)) jh = "0";
				userinfo.setEssence_num(Integer.parseInt(jh));
			} else if (element.toString().contains("帖子：")) {
				String ft = StringUtil.element2text(element.select("li>a").first()).replaceAll("帖", "").replaceAll(" ",
						"");
				if(StringUtil.isNullOrEmpty(ft)) ft = "0";
				userinfo.setPost_num(Integer.parseInt(ft));
				String hf = StringUtil.element2text(element.select("li>a").get(1)).replaceAll("回", "").replaceAll(" ",
						"");
				if(StringUtil.isNullOrEmpty(hf)) hf = "0";
				userinfo.setReply_num(Integer.parseInt(hf));
			} else if (element.toString().contains("注册：")) {
				String createdate_str = StringUtil.element2text(element);
				System.out.println(createdate_str);
			} else if (element.toString().contains("来自：")) {
				String addr_str = StringUtil.element2text(element.select("li>a").first());
				System.out.println(addr_str);
				userinfo.setArea(addr_str);
			}
		}
		System.err.println(StringUtil.objectToJson(userinfo));
	}

	/**
	 * 解析回复内容
	 */
	public void parseReply(String html) {
		Elements replyDiv = Jsoup.parse(html).select("#maxwrap-reply>div.clearfix");
		for (Element element : replyDiv) {
			System.out.println("时间:" + element.attr("data-time"));
			System.out.println("楼层:" + element.attr("rf"));
			// 用户部分
			Element conleft_ele = element.select("div>div.conleft>ul").first();
			// 用户名
			Element user_nickname = conleft_ele.select("ul.maxw>li.txtcenter>a[xname=uname]").first();
			String username = user_nickname.attr("title");

			Element userAtten_ele = conleft_ele.select("ul.maxw>li.txtcenter>a.crade").first();
			if (null != userAtten_ele) {
				//String user_atten = userAtten_ele.attr("title");
			}

			//Element user_help = conleft_ele.select("ul.maxw>li.txtcenter>div.lv_card>span.card").first();
			//String help_ele = user_help.attr("data-target");
			// 用户其他信息
			Element leftlist_ele_userimg = element.select("div>div.conleft>ul.leftlist").first()
					.select("ul>li>p.imguser>a>img").first();
			String userimg = leftlist_ele_userimg.attr("src");
			System.out.println("userIMg:" + userimg);
			System.out.println("昵称:" + username);
			Elements jh_ele = element.select("div>div.conleft>ul.leftlist").first().select("ul>li");
			for (Element element1 : jh_ele) {
				if (element1.toString().contains("精华：")) {
					String jh = StringUtil.element2text(element1.select("li>a").first()).replaceAll("帖", "")
							.replaceAll(" ", "");

					if (StringUtil.isNullOrEmpty(jh)) {
						System.out.println("0");
					} else {
						System.out.println(jh);
					}
				} else if (element1.toString().contains("帖子：")) {
					String ft = StringUtil.element2text(element1.select("li>a").first()).replaceAll("帖", "")
							.replaceAll(" ", "");
					System.out.println(ft);
					String hf = StringUtil.element2text(element1.select("li>a").get(1)).replaceAll("回", "")
							.replaceAll(" ", "");
					System.out.println(hf);
				} else if (element1.toString().contains("注册：")) {
					String createdate_str = StringUtil.element2text(element1);
					System.out.println(createdate_str);
				} else if (element1.toString().contains("来自：")) {
					String addr_str = StringUtil.element2text(element1.select("li>a").first());
					System.out.println(addr_str);
				}
			}

			// 回帖内容
			Element content = element.select("div>div.conright>div.rconten>div.x-reply>div").first();
			System.out.println(StringUtil.element2text(content));
			System.out.println("-------------------------------------------------------------");
			//http://192.168.1.167:8081/services/schedule/addEntry4http
		}
		Element next_page_ele = Jsoup.parse(html).select("#x-pages2>a.afpage").first();
		if (null != next_page_ele) {
			System.err.println(next_page_ele.attr("href"));
		}
	}

	/**
	 * 帖子请求头
	 */
	public Map<String, String> requestHeader(String referer) {
		Map<String, String> requestHeader = new HashMap<String, String>();
		requestHeader.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		requestHeader.put("Accept-Encoding", "gzip, deflate, sdch");
		requestHeader.put("Accept-Language", "zh-CN,zh;q=0.8");
		requestHeader.put("Cache-Control", "max-age=0");
		requestHeader.put("Connection", "keep-alive");
		requestHeader.put("Cookie",
				"CYHTooltip=1; sessionfid=913430254; qasteam21239314=0; RDTaskClose_21239314=1489045427187; cookieCityId=110100; fvlid=1479709625240Zl0kn2IT; sessionid=84D31B8A-7C16-48AC-A35F-F9D9A0990579%7C%7C2016-11-21+14%3A27%3A06.736%7C%7C0; pcpopclub=FC051E5B3C417087A1C827BC4E965812C8304DD4B364F438678771252E9F317B38960EF36D00FD84FAB94C84192A5844B2F1BD283B478EF89FE8A921DDFC6C9523CB19615E03264CE07DA7FDF3E9788560C9C28A5449E6692A8FA1F368FBB930379ABA1E8CCCDA580F354B7A129DFCC526A3DE825C406B0F63135F023251A44FC5FB3F17E60D7C29CE1160369F56F14755A8D66296C778CAFB1377508DD987B53608383D55D9FFDC8142136F05F711B8E6D98370B91F86E1BFDB0B1CCE3B9AE410883C392D6531714AE4582F45FDD91FC814642C5BC5253BBB4EA592B79210540BB4FEEAF864C84F77D9048E2951A7AFD1E6CC9E68DED2C16A4BC7D8FE212B0BFBE80D3315FA802A45FD00261B64819A00E9FE687111E9642944FF3E135B75781079754A608772F4FA71CE8E39F422482815189D0195851AB897CE13D4A8F49A096D7593; clubUserShow=21239314|3207|2|%e6%88%91%e7%9c%9f%e6%98%af%e5%b0%91%e5%85%88%e9%98%9f%e5%91%98|0|0|1|userheaders/g8/M15/F8/E4/120X120_0_q87_autohomecar__wKgHz1eqzkeAbkqJAAFwe0H0duc557.jpg|2016-11-30 10:25:08|0; autouserid=21239314; sessionuid=84D31B8A-7C16-48AC-A35F-F9D9A0990579||2016-11-21+14%3A27%3A06.736||0; sessionip=124.205.63.255; sessionfid=913430254; sessionuserid=21239314; sessionlogin=9376d57490464937abae267a7d5647d901441612; historybbsName4=c-153%7C%E5%AE%9D%E9%A9%AC7%E7%B3%BB%2Cc-117%7C%E8%92%99%E8%BF%AA%E6%AC%A7%2Cc-588%7C%E5%A5%94%E9%A9%B0C%E7%BA%A7%2Cc-2838%7CMacan%2Cc-770%7CRAV4%E8%8D%A3%E6%94%BE%2Cc-375%7C%E9%94%90%E5%BF%97; ahpvno=29; __utma=1.1174897996.1479709627.1489041642.1489049497.41; __utmb=1.0.10.1489049497; __utmc=1; __utmz=1.1489049497.41.26.utmcsr=club.autohome.com.cn|utmccn=(referral)|utmcmd=referral|utmcct=/bbs/forum-c-770-1.html; ref=www.baidu.com%7C0%7C0%7Cwww.google.com%7C2017-03-09+16%3A51%3A06.202%7C2017-02-08+11%3A24%3A19.561; sessionvid=65D34319-E210-48FF-8150-F2205A5C434B; area=110199");
		requestHeader.put("Host", "club.autohome.com.cn");
		requestHeader.put("Referer", referer);
		requestHeader.put("Upgrade-Insecure-Requests", "1");
		requestHeader.put("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
		return requestHeader;
	}
}
